﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Windows;
using Cinch;
using Fluent;
using SLS.Presentation;
using MEFedMVVM.ViewModelLocator;
using SLS.Base;
using SLS.Core.Workspace;
using SLS.Document;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Loc;
using SLS.ExClassLib.MVVM;
namespace SLS.Client
{
    [ViewnameToViewLookupKeyMetadata("MainWindow", typeof(MainWindow))]
    public partial class MainWindow : Window
    { 
        #region =================PROPERTIES=================
        private IUIVisualizerService uiVisualizerService;
        string Address;
        public MainWindowViewModel vm;
        #endregion 

        #region =================MainWindow=================
         
        public  ILocalizationModel LocalizationModel
        {
            get ;
            set ;
        } 
        /// <summary>
        /// Constructor
        /// </summary>
        public MainWindow()
        {
            Tracer.Verbose("MainWindow:MainWindow", "START");  
            try
            {
                Address = "MainWindow";
                LocalizationManager.AppLocalizationProviderType = typeof(VMLocalizationProvider);
                LocalizationManager.CurrentLanguage = (Language)"en";
                LocalizationManager.Initialize();
                InitializeComponent(); 
                DocumentManagement.Instance.LoadAssembly(Assembly.GetExecutingAssembly());
                LocalizationModel = new LanSunStudioSystemLocalizationModel(); 
                EventDispatcher.Instance.OnStatusChange += new EventHandler<StatusEventArgs>(this.MainStatusBar.OnStatusChange);
                EventDispatcher.Instance.OnMessage += new EventHandler<MessageEventArgs>(_LogExplorerDock.OnMessage);
                vm=this.DataContext as MainWindowViewModel;
                #region -------------------- 加载环境变量---------------------
                CheckExcellProcesses();
                dicQueryParamater = new Dictionary<string, object>();
                dicParamater = new Dictionary<string, object>();
                dicQueryParamater.Add("ReadingCommand", null);
                dicQueryParamater.Add("CommandText", null);
                dicQueryParamater.Add("CommandType", null);
                dicQueryParamater.Add("ConnectionString", null);
                dicQueryParamater.Add("Paramaters", null);

                dicParamater.Add("Company", "");
                dicParamater.Add("ExcelFile", null);
                dicParamater.Add("DocType", null);
                dicParamater.Add("OracleDB", null);
                dicParamater.Add("OracleDBConnString", null);
                dicParamater.Add("SqlServerDB", null);
                dicParamater.Add("SqlServerDBConnString", null);

                dicParamater.Add("proxy", null);
                dicParamater.Add("Pwd", "");
                dicParamater.Add("dsParamaters", null);  
                EnvExe.Add("Company", "SLC");
                EnvExe.Add("ExcelFile", "SLCStandard.xls");
                EnvExe.Add("dbSystem", "INTEX1");
                EnvExe.Add("dbUser", "INTEX");
                EnvExe.Add("dbPwd", "INTEX");
                EnvExe.Add("DocType", "SALES");//IMPWS//ARTICLE
                EnvExe.Add("User", "INTEX");
                EnvExe.Add("Pwd", "INTEX");

                EnvExe.Add("DateRange", "1");
                EnvExe.Add("ExCelNameMatrix", null);
                
                strUser = System.Configuration.ConfigurationManager.AppSettings["User"];
                strCompany = System.Configuration.ConfigurationManager.AppSettings["Company"];
                EnvExe = ReadConfig(EnvExe);
                LoadSetSETUP(strCompany, strUser);
                /*
                EnvExe["FTPPwd"] = "intex20" + (System.Convert.ToInt16(EnvExe["FTPPwd"].ToString()) - 1).ToString() + 1;
                EnvExe["FTPPwd"] = "sun";*/
                strUser = (string)EnvExe["User"];
                strCompany = (string)EnvExe["Company"];
                strPwd = (string)EnvExe["Pwd"];
                DocType = (string)EnvExe["DocType"];
                EventArgs e = new EventArgs();
                Connect2DBMenu_Click(this, null);
                #endregion
                #region ------------------设置Themems--------------
                _panel = new System.Windows.Forms.Panel();
                //windowsFormsHost1.Child = _panel; 
                SetUpUserMenu(strCompany, strUser);
                /*
				themes.ItemsSource =WPF.Themes.ThemeManager.GetThemes();
				themes.SelectedItem = "TwilightBlue";
				*/
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                Tracer.Verbose("MainWindow:MainWindow", "END");
            }
        } 
        #endregion  

        #region =================EVENTS=================== 
        private void btnUserSetUp_Click(object sender, RoutedEventArgs e)
        {
            ReSetSETUP(strCompany, strUser);
        }
        private void OfficeWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DesignerProperties.GetIsInDesignMode(this as DependencyObject))
                {
                    return;
                }  
                uiVisualizerService = ViewModelRepository.Instance.Resolver.Container.GetExport<IUIVisualizerService>().Value;
                UILogOn m_SignInAgent = new UILogOn();
                uiVisualizerService.ShowDialog("UILogOn", null); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        #endregion  

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
